<!--
 * @Author: Allen
 * @Date: 2021-01-12 17:44:55
 * @Description: 抱怨身处黑暗，不如提灯前行。愿你在自己存在的地方，成为一束光，照亮世界的一角
 * @LastEditTime: 2021-02-23 11:49:39
 * @LastEditors: Allen
//  收货记录查询
 * @FilePath: \WMS\src\views\WMS\inStorageManage\receivEcordQuery\index.vue
 * @Additional: 附加类容
-->


<template>
  <div class="app-container app-container-table">
    <one-table-template
      v-if="initTemplate"
      ref="multipleTable"
      :dynamicButtons="tableButtons"
      :dynamicComponents="tableComponents"
      :dynamicApiConfig="apiConfig"
      :dynamicTableCols="tableCols"
      :dynamicFormFields="formField"
      :dynamicIsShowSelect="false"
      :dynamicIsColumnDrop="true"
    >
      <template slot="controlBtn" slot-scope="scope">
        <el-link
          type="primary"
          class="el-icon-printer"
          :underline="false"
          @click="print(scope.row)"
          style="font-size:12px;"
        >标签打印</el-link>
      </template>
    </one-table-template>
    <Details :popupsVisible="curPopupsVisible" :key="curPopupsKey" :detailData="detailData"></Details>
    <print v-if="isShow" :rowData="rowData" @detailClose="detailClose" :isShow="isShow" />
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from "@/components/mixins/oneTableWithViewTemplateMixins";
import { onStockApi } from "@/api/wms/onStock";
import OneTableTemplate from "@/components/wms/templates/oneTable";
import request from "@/utils/request";
export default {
  name: "stockQuery",
  // 组件混入对象
  mixins: [oneTableWithViewTemplateMixins],
  components: {
    OneTableTemplate,
    Details: () => import("./detail"),
    print: () => import("./print")
  },
  watch: {
    "formField.WAREHOUSE_IDS": {
      handler: function() {
        var wArr = [];
        if (this.formField.WAREHOUSE_IDS == "") {
          this.tableComponents[0].options.map(item => {
            wArr.push(item.value);
          });
        }
        var WAREHOUSE_IDS_NEW = "";
        WAREHOUSE_IDS_NEW = wArr.join(",");
        if (WAREHOUSE_IDS_NEW != "") {
          this.formField.WAREHOUSE_IDS_NEW = WAREHOUSE_IDS_NEW;
        } else {
          this.formField.WAREHOUSE_IDS_NEW = "";
        }
      }
    }
  },
  created() {
    // if (this.$route.query != null) {
    //   // 判断传入query参数，初始化数据
    //   if (this.$route.query.pageId != null && this.$route.query.pageId !== "") {
    //     this.formField.pageId = this.$route.query.pageId;
    //     this.formField.pageName = this.$route.query.pageName;
    //   }
    // }
    this.querySelect();
  },
  data() {
    return {
      // 网格查询API配置对象
      curPopupsVisible: false,
      curPopupsKey: "",
      apiConfig: onStockApi.queryReceive,
      // 动态组件-按钮
      tableButtons: [
        {
          compKey: "btnKey1",
          type: "primary",
          size: "small",
          clickEvent: () => this.queryTable(),
          name: "search",
          position: "right",
          text: "查询" //"查询"
        },
        {
          compKey: "btnKey3",
          type: "",
          size: "small",
          name: "reset",
          position: "right",
          clickEvent: () => this.reset(),
          text: "重置" //"重置"
        },
        {
          compKey: "btnKey4",
          type: "",
          size: "small",
          clickEvent: () => this.export(),
          name: "export",
          position: "right",
          text: "导出" //"导出"
        }
      ],
      // 动态组件-查询条件
      tableComponents: [
        {
          compKey: "compKey1",
          labelName: "仓库名称", //仓库名称
          codeField: "WAREHOUSE_IDS",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          isMul: true,
          options: [],
          span: 6
        },
        {
          compKey: "compKey2",
          labelName: "收货类型", //收货类型
          codeField: "ARRIVAL_TYPE",
          lookuptype: "PRO0004",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          isMul: false,
          options: [],
          span: 6
        },
        {
          compKey: "compKey3",
          labelName: "打印状态", //紧急标识
          codeField: "IS_PRINT",
          lookuptype: "PRO0004",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          isMul: false,
          span: 6,
          options: [
            {
              label: "未打印",
              value: "0"
            },
            {
              label: "已打印",
              value: "1"
            }
          ]
        },
        {
          compKey: "compKey4",
          labelName: "收货单号",
          codeField: "RECEIVE_CODE",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true,
          span: 6
        },
        {
          compKey: "compKey5",
          labelName: "合作伙伴编号",
          textField: "SUPPLIER_CODE",
          codeField: "SUPPLIER_CODE",
          component: () =>
            import("@/components/wms/wmsEjectWindows/wmsContractInfo"),
          type: "propus",
          span: 6,
          isMust: true,
          isMul: false,
          changed: e => {
            this.formField.SUPPLIER_CODE = e.SUPPLIER_CODE;
            this.formField.PARTNER_SHORT_NAME = e.PARTNER_SHORT_NAME;
          }
        },
        {
          compKey: "compKey6",
          labelName: "合作伙伴名称",
          codeField: "PARTNER_SHORT_NAME",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true,
          disabled: true
        },
        {
          compKey: "compKey7",
          labelName: "备件编号",
          codeField: "PART_NO",
          textField: "PART_NO",
          component: () =>
            import("@/components/wms/wmsEjectWindows/wmsPartInfoTwo"),
          type: "propus",
          span: 6,
          isMust: true,
          returnCodeField: "PART_NO",
          returnTextField: "PART_NO",
          isMul: false
        },
        {
          compKey: "compKey8",
          labelName: "收货日期", //计划发货日期
          codeField: "RECEIVE_TIME_START",
          component: () => import("@/components/wms/datePicker/andtDatePicker"),
          type: "date",
          isMust: true
        },
        {
          compKey: "compKey9",
          labelName: "到货单号",
          codeField: "RELATION_CODE",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        {
          compKey: "compKey10",
          labelName: "待上架查询",
          codeField: "PUTON_STATUS",
          component: () => import("@/components/wms/checkBox"),
          type: "inputText",
          isMust: true
        }
      ],
      // 动态生成网格列
      tableCols: [
        {
          prop: "controlBtn",
          label: "操作",
          codeField: "controlBtn",
          width: 120,
          align: "center",
          isSlot: true
        },
        {
          prop: "WAREHOUSE_NAME",
          label: "仓库名称",
          align: "center",
          width: 100
        },
        {
          prop: "ARRIVAL_NAME",
          label: "收货类型",
          align: "center",
          width: 100
        },
        {
          prop: "RECEIVE_CODE",
          label: "收货单号",
          align: "center",
          width: "120"
        },
        {
          prop: "PARTNER_TYPE",
          label: "合作伙伴类别",
          align: "center",
          width: 100
        },
        {
          prop: "SUPPLIER_CODE",
          label: "合作伙伴编号",
          align: "center",
          width: 100
        },
        {
          prop: "PARTNER_SHORT_NAME",
          label: "合作伙伴简称",
          align: "center",
          width: 100
        },
        {
          prop: "PRINT_STR",
          label: "打印状态",
          align: "center",
          width: 100
        },
        {
          prop: "RECEIVE_STR",
          label: "异常情况",
          align: "center",
          width: 100
        },
        {
          prop: "PART_NO",
          label: "备件编号",
          align: "center",
          width: 100
        },
        {
          prop: "PART_NAME",
          label: "备件名称",
          align: "center",
          width: 100
        },
        {
          prop: "RELATION_CODE",
          label: "关联单号(到货单号)",
          align: "center",
          width: "150"
        },
        {
          prop: "RELATION_LINE_CODE",
          label: "关联单行号(到货单行号)",
          align: "center",
          width: "150"
        },
        {
          prop: "RECEIVE_QTY",
          label: "收货数量",
          align: "center",
          width: 100
        },
        {
          prop: "PUTON_QTY",
          label: "上架数量",
          align: "center",
          width: 100
        },
        {
          prop: "ERROR_QTY",
          label: "异常数量",
          align: "center",
          width: 100
        },
        {
          prop: "SNP_IN",
          label: "采购SNP",
          align: "center",
          width: 100
        },
        {
          prop: "SNP_OUT",
          label: "销售SNP",
          align: "center",
          width: 100
        },
        {
          prop: "SNP_MOVE",
          label: "调拨SNP",
          align: "center",
          width: 100
        },
        {
          prop: "RECEIVE_TIME",
          label: "收货日期",
          align: "center",
          width: "150"
        },
        {
          prop: "CONSIGNEE",
          label: "收货人",
          align: "center",
          width: "150"
        },
        {
          prop: "REMARK",
          label: "备注",
          align: "center",
          width: "150"
        }
      ],
      //表单查询数据
      formField: {
        WAREHOUSE_IDS: "",
        ARRIVAL_TYPE: "",
        IS_PRINT: "",
        RECEIVE_CODE: "",
        SUPPLIER_CODE: "",
        PARTNER_SHORT_NAME: "",
        PART_NO: "",
        RECEIVE_TIME_START: "",
        RELATION_CODE: "",
        PUTON_STATUS: ""
      },
      unFormField: {},
      detailData: {},
      // 控制打印弹窗
      isShow: false,
      rowData: {},
      // 控制初始化页面
      initTemplate: false
    };
  },
  methods: {
    print(row) {
      this.rowData = row;
      console.log( this.rowData);
      this.isShow = true;
    },
    detailClose() {
      this.isShow = false;
    },
    detail(index) {
      const that = this.$refs[this.tableRef];
      this.detailData = {
        PARTNER_SHORT_NAME: that.list[index].PARTNER_SHORT_NAME,
        RECEIVE_CODE: that.list[index].RECEIVE_CODE,
        RECEIVE_ID: that.list[index].RECEIVE_ID
      };
      this.curPopupsVisible = true;
      this.curPopupsKey = this.$utils.generateId();
    },
    querySelect() {
      request({
        url: "/mp/wm/vl/basedata/warehouseuser/queryUserByWHBox.do",
        method: "post",
        data: {
          IS_ENABLE: "1"
        }
      }).then(res => {
        if (res.listRow && res.listRow.length > 0) {
          res.listRow.map(item => {
            this.tableComponents[0].options.push({
              label: item.WAREHOUSE_NAME,
              value: item.WAREHOUSE_ID
            });
          });
          this.formField.WAREHOUSE_IDS = res.listRow[0].WAREHOUSE_ID;
        }
        this.initTemplate = true;
        this.$nextTick(() => {
          this.queryTable();
        });
      });
      request({
        url: "/mp/wm/vl/warehouse/arrival/queryArrivalType.do",
        method: "post",
        data: {}
      }).then(res => {
        res.listRow.map(item => {
          this.tableComponents[1].options.push({
            label: item.ARRIVAL_NAME,
            value: item.ARRIVAL_TYPE
          });
        });
      });
    },
    export() {
      const exportLoading = this.$loading({
        lock: true,
        text: "正在导出...",
        // spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
        fullscreen: true
      });
      request({
        url: "/mp/wm/vl/warehouse/receive/exportReceive.do",
        method: "post",
        headers: {
          "Cache-Control": "no-cache, no-store,max-age=0, must-revalidate",
          Pragma: "no-cache",
          Expires: "0",
          "Content-Type": "application/json;charset=UTF-8"
        },
        responseType: "blob",
        data: this.formField
      })
        .then(response => {
          exportLoading.close();
          let blob = new Blob([response], { type: "application/vnd.ms-excel" }); // 这里表示xmlx类型
          let downloadElement = document.createElement("a");
          let href = window.URL.createObjectURL(blob); // 创建下载的链接
          downloadElement.href = href;
          downloadElement.download = "收货单导出.xls"; // 下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); // 点击下载
          document.body.removeChild(downloadElement); // 下载完成移除元素
          window.URL.revokeObjectURL(href); // 释放掉blob对象
        })
        .catch(err => {
          exportLoading.close();
          console.log(err);
        });
    }
  }
};
</script>
